using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for Limitations
/// </summary>
public class Limitations
{
	public Limitations()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public bool ApplyLimitations(ref DataSet PageInfo, ref DataSet InputInfo, string Operation,ref string limitation_message)
    {
        bool bRet = true;
        try
        {
            foreach (DataRow dr in PageInfo.Tables["Page"].Rows)
            {
                HDDatabase HDD = new HDDatabase();
                string sObjectId = dr["object_id"].ToString();

                string sObject_afrowid = HDD.GetColumnValue("select af_row_id from sy_obj where object_id='" + sObjectId + "'");
                string sRole_afrowid = HDD.GetColumnValue("select role_id from sy_usr_rol where user_id='" + HttpContext.Current.Session["user_afrowid"].ToString() + "'");
                string sRoleId = HDD.GetColumnValue("select role_id from sy_rol where af_row_id='" + sRole_afrowid + "'");
                DataTable dtLimit = HDD.GetDataTable("select * from sy_limitations where object_id='" + sObject_afrowid + "'");
                if (dtLimit != null)
                {
                    foreach (DataRow drLimit in dtLimit.Rows)
                    {
                        if (drLimit["user_id"].ToString() == "" || drLimit["user_id"].ToString() == HttpContext.Current.Session["user_afrowid"].ToString() || drLimit["user_id"].ToString() == HttpContext.Current.Session["userid"].ToString())
                        {
                            if (drLimit["role_id"].ToString() == "" || drLimit["role_id"].ToString() == sRole_afrowid || drLimit["role_id"].ToString() == sRoleId)
                            {
                                if (drLimit["type"].ToString() == "1")
                                {
                                    if (drLimit["number_to_restrict"].ToString() != "")
                                    {
                                        int iRows = int.Parse(drLimit["number_to_restrict"].ToString());
                                        limitation_message = drLimit["override_message"].ToString();
                                        if (drLimit["condition"].ToString() == "")
                                        {
                                            if (Operation == "INSERT")
                                            {
                                                if (HDD.GetDataTable("select * from " + dr["page_table_name"].ToString()).Rows.Count + InputInfo.Tables[dr["page_id"].ToString()].Rows.Count > iRows)
                                                {
                                                    bRet = false;
                                                    break;
                                                }
                                            }
                                            else if (Operation == "UPDATE")
                                            {
                                                DataTable dtTemp = HDD.GetDataTable("select * from " + dr["page_table_name"].ToString() + " where " + drLimit["condition"].ToString());
                                                if (dtTemp.Select(drLimit["condition"].ToString() + " and af_row_id='" + InputInfo.Tables[dr["page_id"].ToString()].Rows[0]["af_row_id"].ToString() + "'").Length > 0)
                                                {
                                                    if (HDD.GetDataTable("select * from " + dr["page_table_name"].ToString() + " where " + drLimit["condition"].ToString()).Rows.Count - InputInfo.Tables[dr["page_id"].ToString()].Select(drLimit["condition"].ToString()).Length > iRows)
                                                    {
                                                        bRet = false;
                                                        break;
                                                    }
                                                }
                                                else if (dtTemp.Select(drLimit["condition"].ToString()).Length + InputInfo.Tables[dr["page_id"].ToString()].Select(drLimit["condition"].ToString()).Length > iRows)
                                                {
                                                    bRet = false;
                                                    break;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (Operation == "INSERT")
                                            {
                                                if (HDD.GetDataTable("select * from " + dr["page_table_name"].ToString() + " where " + drLimit["condition"].ToString()).Rows.Count + InputInfo.Tables[dr["page_id"].ToString()].Select(drLimit["condition"].ToString()).Length > iRows)
                                                {
                                                    bRet = false;
                                                    break;
                                                }
                                            }
                                            else if (Operation == "UPDATE")
                                            {
                                                DataTable dtTemp = HDD.GetDataTable("select * from " + dr["page_table_name"].ToString() + " where " + drLimit["condition"].ToString());
                                                if (dtTemp.Select(drLimit["condition"].ToString() + " and af_row_id='" + InputInfo.Tables[dr["page_id"].ToString()].Rows[0]["af_row_id"].ToString() + "'").Length > 0)
                                                {
                                                    if (HDD.GetDataTable("select * from " + dr["page_table_name"].ToString() + " where " + drLimit["condition"].ToString()).Rows.Count - InputInfo.Tables[dr["page_id"].ToString()].Select(drLimit["condition"].ToString()).Length > iRows)
                                                    {
                                                        bRet = false;
                                                        break;
                                                    }
                                                }
                                                else if (dtTemp.Select(drLimit["condition"].ToString()).Length + InputInfo.Tables[dr["page_id"].ToString()].Select(drLimit["condition"].ToString()).Length > iRows)
                                                {
                                                    bRet = false;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        catch (Exception Ex)
        {
            LogWriter.WriteLog("ApplyLimitations : Error : " + Ex.Message);
        }
        return bRet;
    }
}
